'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 公会
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var modelUtil = require('../util/modelUtil');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var unionGroupSchema = require('../schemas/unionGroupSchema');
var UnionGroup = mongoConnector().model('UnionGroup');

/**
 * 根据id获取军团
 * @param  {[type]}   union_id [description]
 * @param  {Function} cb       [description]
 * @return {[type]}            [description]
 */
module.exports.getById = function (union_id, cb) {
    UnionGroup.findOne({
        union_id: union_id
    }, function (err, union_group_model) {
        if (!!err) {
            console.error(err);
            return cb();
        } else if (!!union_group_model) {
            // _resetUnionGroup(union_group_model);
        }
        cb(null, union_group_model);
    });
};

/**
 * 根据名字获取军团
 * @param  {[type]}   union_name [description]
 * @param  {Function} cb         [description]
 * @return {[type]}              [description]
 */
module.exports.getByName = function (union_name, cb) {
    UnionGroup.findOne({
        name: union_name
    }, cb);
};

/**
 * 获取軍團列表
 * @param  {Function} cb       [description]
 * @return {[type]}            [description]
 */
module.exports.getUnionList = function (cb) {
    UnionGroup.find({})
        .limit(20)
        .select('union_id name union_announce limit_level lv union_flag apply_list union_user_list union_exp')
        .sort({
            union_exp: -1,
            union_id: -1
        })
        .exec(cb);
};

/**
 * 查询联盟排名
 */
module.exports.getRankByUnionId = function (union_id, union_exp, cb) {
    UnionGroup.count({
            union_exp: {
                $gt: union_exp
            }
        })
        .exec(function (err, greate_count) {
            UnionGroup.count({
                    union_exp: union_exp,
                    union_id: {
                        $gt: union_id
                    }
                })
                .exec(function (err, equals_count) {
                    cb(err, greate_count + equals_count + 1);
                });
        });
};

/**
 * 根据军团名字关键字获取军团
 * @param  {[type]}   union_id [description]
 * @param  {Function} cb       [description]
 * @return {[type]}            [description]
 */
module.exports.getByNameKeyWord = function (word, cb) {
    UnionGroup.find({
            name: {
                $regex: word
            }
        })
        .limit(20)
        .select('union_id name union_announce limit_level lv union_flag apply_list')
        .exec(cb);
};

/**
 * 创建公会
 * @param  {[type]}   union_name [description]
 * @param  {Function} cb         [description]
 * @return {[type]}              [description]
 */
module.exports.create = function (union_name, role_id, cb) {
    UnionGroup.create({
        name: union_name,
        create_time: moment(),
        create_role_id: role_id
    }, cb);
};

/**
 * 解散公会
 * @param  {[type]}   union_id [description]
 * @param  {Function} cb       [description]
 * @return {[type]}            [description]
 */
module.exports.dismiss = function (union_id, cb) {
    UnionGroup.remove({
        union_id: union_id
    }, function (err) {
        if (!!err) {
            console.error(err);
        }
    });
};
